<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_TagName_FormItem"
								prop="Entity.TagName"
								:rules="[{ required: true, message: '标签名称为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="标签名称" tooltip="标签名称" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.TagName" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_TagCode_FormItem" prop="Entity.TagCode">
								<template #label>
									<WtmLabelTooltip label="标签编码" tooltip="标签编码，用于系统内部标识" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.TagCode" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Description_FormItem" prop="Entity.Description">
								<template #label>
									<WtmLabelTooltip label="标签描述" tooltip="标签的详细描述" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Description" disabled class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>显示设置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Color_FormItem" prop="Entity.Color">
								<template #label>
									<WtmLabelTooltip label="标签颜色" tooltip="标签显示颜色" />
								</template>
								<el-color-picker v-model="stateData.vmModel.Entity.Color" disabled class="width100"></el-color-picker>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SortOrder_FormItem" prop="Entity.SortOrder">
								<template #label>
									<WtmLabelTooltip label="排序" tooltip="标签显示排序，数值越小越靠前" />
								</template>
								<el-input-number v-model="stateData.vmModel.Entity.SortOrder" disabled class="width100"></el-input-number>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>状态设置</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_IsActive_FormItem"
								prop="Entity.IsActive"
								:rules="[{ required: true, message: '是否启用为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="是否启用" tooltip="是否启用此标签" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.IsActive" disabled class="width100">
									<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
									<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
								</el-select>
							</el-form-item>
						</el-col>
					</el-row>
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysArticleTagApi as api } from '/@/views/article/sysarticletag/api';
import other from '/@/utils/other';
import { useRouter } from 'vue-router';
import { ISysArticleTagVm } from '/@/views/article/sysarticletag/api/interface';
import { IViewModel } from '/@/types/common';

// 常量定义
const BILL_STATUS = {
	DRAFT: '草稿',
	IN_AUDIT: '审核中',
} as const;

const WORKFLOW_STATUS = {
	PENDING: '待提交',
	SUBMITTING: '提交中',
	IN_AUDIT: '审核中',
} as const;

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;

const ACTIONS = {
	AGREE: '同意',
	REJECT: '拒绝',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysArticleTagVm>>({
	vmModel: {
		Entity: {
			ID: null,
			TagName: null,
			TagCode: null,
			Description: null,
			Color: null,
			SortOrder: null,
			IsActive: false,
		},

		ActionName: null,
		IsMyApprove: null,
	},
});

// 统一的成功处理函数
const handleApiResponse = (response: any, isSubmit = false) => {
	if (isSubmit) {
		ElMessage.success(ci.proxy?.$t('message._system.common.vm.submittip') || '提交成功');
		emit('refresh');
		closeDialog();
	} else {
		ElMessage.success(ci.proxy?.$t('message._system.common.vm.savetip') || '保存成功');
		emit('refresh');
	}
	loading.value = false;
};

// 统一的错误处理函数
const handleError = (error: any) => {
	other.setFormError(ci, error);
	loading.value = false;
};

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

// 初始化页面数据
const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
